import * as types from '../action/ActionTypes';

const defaultState = {
  userInfo: '',
  currentRoom: null,
  speakers: [],
  listeners: [],
  offlineUser: [],
  muteUsers: [], //静音用户列表，
  speakingUsers: [], //正在说话的用户,
  raisedHandUsers: [], //举手要发言的用户
  chatDatas: [], //聊天记录
};

export default function userReducer(state = defaultState, action) {
  // console.log(action);
  switch (action.type) {
    case types.UPDATE_USER_INFO:
      return {
        ...state,
        userInfo: action.userInfo,
      };
    case types.CURRENT_ROOM:
      return {
        ...state,
        currentRoom: action.room,
      };
    case types.CURRENT_ROOM_SPEAKER:
      return {
        ...state,
        speakers: action.users || [],
      };
    case types.CURRENT_ROOM_LISTENER:
      return {
        ...state,
        listeners: action.users || [],
      };
    case types.CURRENT_ROOM_OFFLINEUSER: {
      // let newUsers = state.offlineUser.filter(
      //   (item) => item.id != action.data.user.id,
      // );

      // if (action.data.type == 'add') {
      //   newUsers.push(action.data.user);
      // }

      // if (action.data.type == 'clear') {
      //   newUsers = [];
      // }

      // console.log(action.data);
      // console.log(newUsers);

      return {
        ...state,
        offlineUser: action.data.users || [],
      };
    }
    case types.ROOM_MUTE_USERT:
      return {
        ...state,
        muteUsers: action.users || [],
      };
    case types.ROOM_SPEAKING_USERS:
      return {
        ...state,
        speakingUsers: action.users || [],
      };
    case types.ROOM_RAISE_HAND_USERS:
      return {
        ...state,
        raisedHandUsers: action.users || [],
      };
    case types.CURRENT_ROOM_CHAT_DATA:
      return {
        ...state,
        chatDatas: action.datas || [],
      };
    default:
      return state;
  }
}
